<?php
/*
 * @version      SVN: $Id: test.php 46 2009-04-21 04:54:04Z M.Kamashev $
 */
    define (INDEX,'/');
    define('APPPATH',dirname(__FILE__));
    
    header('Content-Type: text/html; charset=utf-8');

    require_once 'system/functions.php';
    require_once '../../simpletest/autorun.php';

    /**
     *   FIXME Разобраться с исключениями в SimpleTest, и покрыть тестами ситуации с исключениями
     */
    class Unit_Test_Bookmark extends UnitTestCase {

        /**
         * test_construct_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_construct_Bookmark() {
            $this->assertTrue(new Bookmark(
                'http://floscoeli.com/', 
                'Floscoeli.com Homepage', 
                'Моя домашняя страничка', 
                array('floscoeli', 'homepage', 'site', 'тег на русском с пробелами')
            ), "constructor with valid data");
            
            $this->assertTrue(new Bookmark(
                '', 
                'Floscoeli.com Homepage', 
                'Моя домашняя страничка', 
                array('floscoeli', 'homepage', 'site', 'тег на русском с пробелами')
            ),"Empty URL");
          
            $this->assertTrue(new Bookmark(
                'http://floscoeli.com/', 
                '', 
                'Моя домашняя страничка', 
                array('floscoeli', 'homepage', 'site', 'тег на русском с пробелами')
            ),"Empty title");
            
            $this->assertTrue(new Bookmark(
                'http://floscoeli.com/', 
                'Floscoeli.com Homepage', 
                '', 
                array('floscoeli', 'homepage', 'site', 'тег на русском с пробелами')
            ),"Empty description");

            $this->assertTrue(new Bookmark(
                'http://floscoeli.com/', 
                'Floscoeli.com Homepage', 
                'Моя домашняя страничка', 
                array()
            ),"Not tags");

            $this->assertTrue(new Bookmark(
                'http://floscoeli.com/', 
                'Floscoeli.com Homepage', 
                'Моя домашняя страничка', 
                array('', 'one', '', 'tags')
            ),"with empty tags");
            
            $this->assertTrue(new Bookmark(), "without params");
        }

        /**
         * test_title_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_title_Bookmark() {
            $bookmark = new Bookmark(); 
            //$this->assertFalse($bookmark->setTitle(NULL));
            //$this->expectError(new PatternExpectation("/empty title/"));
            $this->assertTrue($bookmark->setTitle('123'));
            $this->assertTrue($bookmark->setTitle(123));
            $this->assertTrue($bookmark->setTitle(TRUE));
            $this->assertTrue($bookmark->setTitle('sdf'));
            //$this->assertFalse($bookmark->setTitle(FALSE));
            $this->assertPattern("/^(.*)/iu",$bookmark->getTitle());
        }

        /**
         * test_url_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_url_Bookmark() {
            $bookmark = new Bookmark(); 
            $this->assertTrue($bookmark->setUrl('floscoeli.com'));
            $this->assertTrue($bookmark->setUrl('http://floscoeli.com'));
            $this->assertTrue($bookmark->setUrl('ftp://floscoeli.com'));
            $this->assertTrue($bookmark->setUrl('smb://floscoeli.com'));
            $this->assertTrue($bookmark->setUrl('file:///home/ukko/123.html'));
            $this->assertTrue($bookmark->setUrl('\\winsrv\public\124.html'));
            //$this->assertFalse($bookmark->setUrl('floscoeli'));
            $this->assertPattern("/\w/iu", $bookmark->getUrl());
        }

        /**
         * test_date_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_date_Bookmark () {
            $bookmark = new Bookmark();
            $this->assertTrue($bookmark->setDate(time()));
            $this->assertTrue($bookmark->setDate(123412));
            //$this->assertFalse($bookmark->setDate(-1));
            //$this->assertTrue($bookmark->setDate(0));
            //$this->assertTrue($bookmark->setDate('Сегодня'));
            $this->assertTrue($bookmark->getDate() > 0);
        }

        /**
         * test_description_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_description_Bookmark() {            
            $bookmark = new Bookmark();
            $this->assertTrue($bookmark->setDescription("Мама, мыла раму"));
            $this->assertTrue($bookmark->setDescription("<b>Пишем</b> <script>текст с</script> & тегами"));
            $this->assertPattern("/\w/iu",$bookmark->getDescription());
        }

        /**
         * test_description_Bookmark 
         * 
         * @access public
         * @return void
         */
        public function test_tags_Bookmark() {
            $bookmark = new Bookmark();
            $this->assertTrue($bookmark->setTags(array('asd','asf','fff')));
            //$this->assertTrue($bookmark->setTags(array()));
            $this->assertTrue($bookmark->addTag('asd'));
            $this->assertIsA($bookmark->getTags(),array());
        }
    } 

    /**
     *  Тестируются только дописанные методы, методы, которые наслудуются от интерфейся, нет смысла тестировать.. Считаю..
     */
    class Unit_Test_Bookmarks extends UnitTestCase {

        /**
         * test_add_Bookmarks 
         * 
         * @access public
         * @return void
         */
        public function test_add_Bookmarks() {
            $bookmarks = new Bookmarks();
            
            $this->assertIsA(
                $bookmarks->add(
                    'floscoeli.com/', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "without prefix http://"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com/', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "normal"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'ftp://floscoeli.com/', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "protocol prefix ftp"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'smb://floscoeli.com/', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "protocol prefix smb"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'file:///C:/PROGRAMS/WS/', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "protocol prefix local file"
            );

            $this->assertIsA(
                $bookmarks->add(
                    '\\computer\file', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "protocol prefix windows network file"
            );

            $this->assertTrue(
                $bookmarks->add(
                    'as,q.wqazzx', 'Floscoeli',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                "Error uri"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com', '',12312154, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "without title"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com/', 'Floscoeli','', 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "without date"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com/', 'Floscoeli', 0, 'Описание сайта',array('site', 'floscoeli')
                ),
                Bookmark, "empty date"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com/', 'Floscoeli', 12312154, '', array('site', 'floscoeli')
                ),
                Bookmark, "empty description"
            );

            $this->assertIsA(
                $bookmarks->add(
                    'http://floscoeli.com/', 'Floscoeli', 12312154, 'Описание сайта', array()
                ),
                Bookmark, "tags - empty array"
            );
        }

        /**
         * test_clear_Bookmarks 
         * 
         * @access public
         * @return void
         */
        public function test_clear_Bookmarks() {
            $bookmarks = new Bookmarks();
            $bookmarks->add('http://floscoeli.com/', 'Floscoeli',12312154, 'Описание сайта', array('site', 'floscoeli'));

            $this->assertTrue($bookmarks->clear());
        }

        /**
         * test_saveXML_Bookmarks 
         * 
         * @access public
         * @return void
         */
        public function test_saveXML_Bookmarks() {
            $file = dirname(__FILE__).'/cache/'.MD5('ukko');
            $dom = new DOMDocument();
            $bookmarks = new Bookmarks();
            $bookmarks->add('http://floscoeli.com/', 'Floscoeli', 12312154, 'Описание сайта', array('site', 'floscoeli'));
            $bookmarks->add('http://google.com/', 'Google', 12332154, 'Поисковик Гугл', array('site', 'google', 'search'));
            @unlink($file);
            $this->assertTrue($bookmarks->saveXML($file), 'save XML');
            $this->assertTrue($dom->load($file), "valid XML");
        }

        /**
         * test_loadXML_Bookmarks 
         * 
         * @access public
         * @return void
         */
        public function test_loadXML_Bookmarks() {           
            $file = dirname(__FILE__).'/cache/'.MD5('ukko');
            $bookmarks = new Bookmarks();
            if (!file_exists($file)) {
                $bookmarks->add('http://floscoeli.com/', 'Floscoeli', 12312154, 'Описание сайта', array('site', 'floscoeli'));
                $bookmarks->add('http://google.com/', 'Google', 12332154, 'Поисковик Гугл', array('site', 'google', 'search'));
                $bookmarks->saveXML($file);
            }
            $this->assertTrue($bookmarks->loadXML($file), "Load XML");
            //TODO Сделать валидацию XML 
        }
   }
?>
